package com.chenwenxuan.sync.ui.editor;

import java.awt.event.ActionEvent;

import javax.swing.tree.DefaultMutableTreeNode;

import com.chenwenxuan.sync.console.model.TaskCatalog;
import com.chenwenxuan.sync.ui.AbstractAction;
import com.chenwenxuan.sync.ui.AppWindow;
import com.chenwenxuan.sync.ui.ImageIconResources;
import com.chenwenxuan.sync.ui.MenuManager;

public class OpenTaskCatalogEditorAction extends AbstractAction {

	private static final long serialVersionUID = 888720679113109108L;


	private ImageIconResources imageIconResources;

	public OpenTaskCatalogEditorAction(String name, String icon, ImageIconResources imageIconResources) {
		super(name, imageIconResources.getIcon(icon));
		this.imageIconResources = imageIconResources;
	}

	@Override
	public void fillNavMenu(MenuManager menuManager, DefaultMutableTreeNode [] selected) {
		if ( selected.length != 1 ) 
			return;
		
		Object sel = selected[0].getUserObject();
		if ( !(sel instanceof TaskCatalog) ) {
			return;
		}
		
		if ( !menuManager.contains(this)) {
			menuManager.add(this);
		}
	
	}

	@Override
	public void doIt(ActionEvent e) {
		AppWindow.getAppNavigation().getSelectedNode();
		
		
		Object obj = AppWindow.getAppNavigation().getSelectedNode().getUserObject();
		
		if ( ! (obj instanceof TaskCatalog) ) {
			return;
		}
		
		TaskCatalogEditor editor = new TaskCatalogEditor();
		editor.setInput(obj);
		
		AppWindow.openInEditorPart(editor);
	}

	@Override
	public boolean canOpenEditor(Object userObject) {
		return userObject instanceof TaskCatalog;
	}

	@Override
	public boolean isDefaultAction() {
		return true;
	}

	public ImageIconResources getImageIconResources() {
		return imageIconResources;
	}

	public void setImageIconResources(ImageIconResources imageIconResources) {
		this.imageIconResources = imageIconResources;
	}



}
